
<template>
  <div class="xtx-cart-page">
    <div class="container m-top-20">
      <div class="cart">
        <table>
          <thead>
            <tr>
              <th width="120">
                <el-checkbox
                  :model-value="cartStore.isAll"
                  @change="allCheck"
                />
              </th>
              <th width="400">商品信息</th>
              <th width="220">单价</th>
              <th width="180">数量</th>
              <th width="180">小计</th>
              <th width="140">操作</th>
            </tr>
          </thead>
          <!-- 商品列表 -->
          <tbody>
            <tr v-for="i in cartStore.cartList" :key="i.id">
              <td>
                <!-- @change必须要这样写，高阶函数，函数中返回另一个函数 内部会调用第一个函数传递当前的checkbox的val，所以你用一个seleced接受他，然后传递给内部的自己定义的函数即可 -->
                <el-checkbox
                  :model-value="i.selected"
                  @change="(selected) => singleCheck(i, selected)"
                />
              </td>
              <td>
                <div class="goods">
                  <RouterLink to="/"
                    ><img :src="i.picture" alt=""
                  /></RouterLink>
                  <div>
                    <p class="name ellipsis">
                      {{ i.name }}
                    </p>
                  </div>
                </div>
              </td>
              <td class="tc">
                <p>&yen;{{ i.price }}</p>
              </td>
              <td class="tc">
                <el-input-number v-model="i.count" />
              </td>
              <td class="tc">
                <p class="f16 red">&yen;{{ (i.price * i.count).toFixed(2) }}</p>
              </td>
              <td class="tc">
                <p>
                  <el-popconfirm
                    title="确认删除吗?"
                    confirm-button-text="确认"
                    cancel-button-text="取消"
                    @confirm="delCart(i)"
                  >
                    <template #reference>
                      <a href="javascript:;">删除</a>
                    </template>
                  </el-popconfirm>
                </p>
              </td>
            </tr>
            <tr v-if="cartStore.cartList.length === 0">
              <td colspan="6">
                <div class="cart-none">
                  <el-empty description="购物车列表为空">
                    <el-button type="primary">随便逛逛</el-button>
                  </el-empty>
                </div>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <!-- 操作栏 -->
      <div class="action">
        <div class="batch">
          共 {{ cartStore.allCount }} 件商品，已选择
          {{ cartStore.selectedCount }} 件，商品合计：
          <span class="red">¥ {{ cartStore.selectedPrice.toFixed(2) }} </span>
        </div>
        <div class="total">
          <el-button
            size="large"
            type="primary"
            @click="$router.push('/checkout')"
            >下单结算</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useCartStore } from "@/stores/cartStore";
const cartStore = useCartStore();

const singleCheck = (i, selected) => {
  // store cartList 数组，无法知道要修改谁的选中状态
  cartStore.singleCheck(i.skuId, selected);
};

// 全选
const allCheck = (selected) => {
  cartStore.allCheck(selected);
};
</script>


<style scoped lang="scss">
.xtx-cart-page {
  margin-top: 1.25rem;

  .cart {
    background: #fff;
    color: #666;

    table {
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 1.5rem;

      th,
      td {
        padding: 0.625rem;
        border-bottom: 0.0625rem solid #f5f5f5;

        &:first-child {
          text-align: left;
          padding-left: 1.875rem;
          color: #999;
        }
      }

      th {
        font-size: 1rem;
        font-weight: normal;
        line-height: 3.125rem;
      }
    }
  }

  .cart-none {
    text-align: center;
    padding: 7.5rem 0;
    background: #fff;

    p {
      color: #999;
      padding: 1.25rem 0;
    }
  }

  .tc {
    text-align: center;

    a {
      color: $xtxColor;
    }

    .xtx-numbox {
      margin: 0 auto;
      width: 7.5rem;
    }
  }

  .red {
    color: $priceColor;
  }

  .green {
    color: $xtxColor;
  }

  .f16 {
    font-size: 1rem;
  }

  .goods {
    display: flex;
    align-items: center;

    img {
      width: 6.25rem;
      height: 6.25rem;
    }

    > div {
      width: 17.5rem;
      font-size: 1rem;
      padding-left: 0.625rem;

      .attr {
        font-size: 0.875rem;
        color: #999;
      }
    }
  }

  .action {
    display: flex;
    background: #fff;
    margin-top: 1.25rem;
    height: 5rem;
    align-items: center;
    font-size: 1rem;
    justify-content: space-between;
    padding: 0 1.875rem;

    .xtx-checkbox {
      color: #999;
    }

    .batch {
      a {
        margin-left: 1.25rem;
      }
    }

    .red {
      font-size: 1.125rem;
      margin-right: 1.25rem;
      font-weight: bold;
    }
  }

  .tit {
    color: #666;
    font-size: 1rem;
    font-weight: normal;
    line-height: 3.125rem;
  }
}
</style>